package com.exsun.ex1077.ui.activity.device

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.extend.photoUrlToSrcUrl
import com.exsun.ex1077.extend.toPhotoUrl
import com.exsun.ex1077.model.Device
import com.exsun.ex1077.model.LabelValue
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.Photo
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField
import com.zhangping.fastjetpack.extend.toFileFormPart
import com.zhangping.fastjetpack.extend.toJson
import com.zhangping.fastjetpack.model.KeyValue
import com.zhangping.fastjetpack.model.Tree

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2023/3/24 14:12
 */
class DeviceAddViewModel: BaseViewModel() {

    var device: Device? = null

    val pageType = IntObservableField()
    var deviceModelList: List<LabelValue>? = null
    var manageTypelList: List<KeyValue>? = null
    var equipmentId: String? = null
    val deviceNo = StringObservableField()
    var companyId = StringObservableField()
    var companyName = StringObservableField()
    var deviceModel: Int? = null
    var deviceModelName = StringObservableField()
    var simId = StringObservableField()
    var manageType = IntObservableField(-1)
    var manageTypeName = StringObservableField()
    var videoChannel = StringObservableField()
    var objectIcon: String? = null
    var vehicleId: String? = null
    var objectName = StringObservableField()
    var objectSex: Int = -1
    var objectModel = StringObservableField()
    var objectColor = StringObservableField()
    var personId = StringObservableField()
    var personName = StringObservableField()
    var personTel = StringObservableField()
    var age = StringObservableField()
    var idCard = StringObservableField()

    val deviceNoCheckResult = MutableLiveData<ResultState<Device?>>()
    val simCheckResult = MutableLiveData<ResultState<Any?>>()
    val deptTreeResult = MutableLiveData<ResultState<List<Tree>>>()
    val saveResult = MutableLiveData<ResultState<Any?>>()
    val updateResult = MutableLiveData<ResultState<Any?>>()
    val deviceDataResult = MutableLiveData<ResultState<Device?>>()

    fun deviceNoCheck(deviceNo: String) {
        request({ apiService.deviceNoCheck(deviceNo)}, deviceNoCheckResult)
    }

    fun loadDeptTree(){
        request({ apiService.vehicleDeptTree()}, deptTreeResult)
    }

    fun loadDeviceModel(){
        request({ apiService.deviceModelList()}, {
            deviceModelList = it
        })
    }

    fun loadManageTypeList(){
        request({ apiService.manageTypeList()}, {
            manageTypelList = it
        })
    }

    fun simCheck(sim: String){
        request({ apiService.deviceSimCheck(equipmentId ?: "", sim)}, simCheckResult)
    }

    fun objectPhotoUpload(path: List<String>): MutableLiveData<List<String>>{
        val liveData = MutableLiveData<List<String>>()
        request({ apiService.deviceObjectPhotoUpload(path.map { it.toFileFormPart() })}, {
            liveData.value = it
        }, isShowDialog = true)
        return liveData
    }

    fun personPhotoUpload(path: List<String>): MutableLiveData<List<String>>{
        val liveData = MutableLiveData<List<String>>()
        request({ apiService.devicePersonPhotoUpload(path.map { it.toFileFormPart() })}, {
            liveData.value = it
        }, isShowDialog = true)
        return liveData
    }

    fun submit(objectPhoto: MutableList<Any>, personPhoto: MutableList<Any>){
        val body = JSONObject().apply {
            put("equipmentId", equipmentId)
            put("deviceId", deviceNo.get())
            put("companyId", companyId.get())
            put("deviceModel", deviceModel)
            if (simId.get().isNotEmpty()){
                put("simId", simId.get())
            }
            put("objType", manageType.get())
            if (videoChannel.get().isNotEmpty()){
                put("channelsNum", videoChannel.get())
            }
            put("objIcon", objectIcon)
            put("numberPlate", objectName.get())
            if (!vehicleId.isNullOrEmpty()){
                put("vehicleId", vehicleId)
            }
            when(manageType.get()){
                Constant.OBJECT_TYPE_VEHICLE, Constant.OBJECT_TYPE_SHIP -> {
                    //车辆和船舶
                    if (objectModel.get().isNotEmpty()){
                        put("vehicleModel", objectModel.get())
                    }
                    if (objectColor.get().isNotEmpty()){
                        put("vehicleColor", objectColor.get())
                    }
                }
                Constant.OBJECT_TYPE_PERSON -> {
                    //人员
                    if (objectSex >= 0){
                        put("ownerSex", objectSex)
                    }
                    if (objectModel.get().isNotEmpty()){
                        put("contactNumberVehicle", objectModel.get())
                    }
                    if (objectColor.get().isNotEmpty()){
                        put("ageVehicle", objectColor.get())
                    }
                }
            }
            if (personId.get().isNotEmpty()){
                put("driverId", personId.get())
            }
            if (personName.get().isNotEmpty()){
                put("name", personName.get())
            }
            if (personTel.get().isNotEmpty()){
                put("contactNumber", personTel.get())
            }
            if (age.get().isNotEmpty()){
                put("age", age.get())
            }
            if (idCard.get().isNotEmpty()){
                put("idCard", idCard.get())
            }
            //添加监管对象图片
            put("vehiclePicStr", objectPhoto.map { Photo(it.toString().photoUrlToSrcUrl()) }.toJson())
            //添加负责人图片
            put("driverPicStr", personPhoto.map { Photo(it.toString().photoUrlToSrcUrl()) }.toJson())
        }
        if (pageType.get() == Constant.DEVICE_MANAGE_ADD){
            request({ apiService.deviceSave(body)}, saveResult)
        }else if (pageType.get() == Constant.DEVICE_MANAGE_UPDATE){
            request({ apiService.deviceUpdate(body)}, updateResult)
        }
    }

    fun loadDeviceData(){
        request({ apiService.deviceQuery(equipmentId!!)}, deviceDataResult, true)
    }
}